const GameData = require("GameData");


cc.Class({
    extends: cc.Component,
    properties: {
        
        mPrefab: {
            default: null,
            type:cc.Prefab,    
        },

        mFishPool: {
            default: null,
            type:cc.Node,
        },


        mHook: {
            default: null,
            type:cc.Node,
            tooltip: "鱼钩",            
        },

        mDepth:{
            default: 0,
            type: cc.Float,
            visible: false,
            tooltip:"当前深度",
        },


        mSceneData:{
            default: null,
            type:cc.JsonAsset,
        },

        mIndex:{
            default: 0,
            visible: false,
            tooltip:"我也不知道这是什么",
        },

        mController:{
            default: null,
            type:cc.Node,
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.cx = cc.winSize.width /2;
        this.cy = cc.winSize.height /2;

        var manager = cc.director.getCollisionManager();
        manager.enabled = true;
        // manager.enabledDebugDraw = true;
    },

    start () {

    },

    update (dt) {

        let depth = Math.abs(this.mHook.y) / 100 ;  // 100 pixel = 1m
        this.mDepth = Math.floor(depth) ;  // 100 pixel = 1m
        let data = this.mSceneData.json[this.mIndex];
        if(data != undefined && this.mDepth >= data.depth){
        // if(depth - this.mDepth > 0){
            // 生成一个新的鱼
            let fish = cc.instantiate(this.mPrefab);            
            cc.resources.load(data.res,cc.SpriteFrame,function(err,spriteFrame){
                if(!err){
                    fish.getComponent(cc.Sprite).spriteFrame = spriteFrame;
                }
            })
            fish.x = Math.random() * cc.winSize.width - this.cx;
            fish.y = this.mHook.y - this.cy - 100;
            this.mFishPool.addChild(fish);
            this.mIndex++;
        }
        
        GameData.instance.depth = Math.max(GameData.instance.depth ,depth);        
    },

    reset() {
        GameData.instance.reset();
        this.mIndex = 0;
        this.mFishPool.removeAllChildren(true);
        this.mController.removeAllChildren(true);
        this.mController.x = 0;        
    }
});
